// pagesProperty/pages/survey/add/index.js
var app = getApp(), fetchApi = require("../../../../utils/httpPost.js"), 
common_Js = require("../../../../component/common.js");
import Toast from '../../../../component/vant/toast/toast';
import Dialog from '../../../../component/vant/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    currentTab:0,
    title:'',
    conent:'',
    survey_type:1,
    titlePlaceholder:'请输入公投标题',
    contentPlaceholder:'请输入公投内容',
    submitState:false,
    isEdit:0,
    survey_id:0,
    survey:{},
    isLoading:true,
    questions:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.id){
      this.data.survey_id = parseInt(options.id);
    }
    if(options.isEdit){
      this.data.isEdit = parseInt(options.isEdit);
    }else{
      this.setData({
        isLoading: false,
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if(this.data.isEdit == 1){
      this.onHttpInit();
    }
  },
  handleChange({ detail }) {
    console.log('handleChange',detail)
    var that = this;
    that.setData({
      currentTab: detail.index,
    });
  },
  goAddQuestion(){
    if(this.data.survey_id == 0 || this.data.survey_id == undefined){
      Toast("请先保存公投信息");
      return;
    }
    wx.navigateTo({
      url: '/pagesProperty/pages/survey/addQuestion/index?s_id='+this.data.survey.id,
    })
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/survey/question/list', {
      survey_id : that.data.survey_id,
    }, function (res) {
      if (res.data.code == 0) {
        that.data.questions = res.data.data.questions;
        that.setData({
          survey: res.data.data.survey,
          questions: res.data.data.questions,
          title: res.data.data.survey.questionTitle,
          content: res.data.data.survey.question,
        })
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  bindinput(e){
    console.log('bindinput',e);
    var key = e.currentTarget.dataset.key;
    var detail = e.detail.value;
    if(key == 'title'){
      this.data.title = detail;
    }else{
      this.data.content = detail;
    }
  },
  goEdit(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pagesProperty/pages/survey/addQuestion/index?s_id=' + this.data.survey_id + '&q_id=' + id,
    })
  },
  trim: function (t) {
    return t.replace(/(^\s*)|(\s*$)/g, "");
  },
  submitOp:function(){
    console.log('123123')
    if(this.data.title == ''){
      Toast.fail('公投标题不能为空');
      return;
    }
    if(this.data.content == ''){
      Toast.fail('公投内容不能为空');
      return;
    }
    var that = this, title = this.trim(that.data.title),
    content = this.trim(that.data.content);
    if (title.length <= 0){
      Toast.fail('公投标题不能为空');
      return;
    }
    
    if (content.length <= 0){
      Toast.fail('公投内容不能为空');
      return;
    }

    if(that.data.submitState){
      Toast.fail('正在提交...');
      return;
    }
    that.data.submitState = true;
    that.setData({submitState : true })

    fetchApi.httpPost('/survey/add', {
      title: title,
      content: content,
      survey_type: that.data.survey_type,
      survey_id:that.data.survey_id,
    }, function (res) {
      console.log("公投报返回数据", res)
      if (res.data.code == 0) {
        that.data.survey = res.data.data;
        that.data.survey_id = res.data.data.id;

        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.success(res.data.msg);
        that.setData({
          survey:res.data.data,
          title: res.data.data.questionTitle,
          content: res.data.data.question,
          currentTab: 1,
        })
      }else{
        that.data.submitState = false;
        that.setData({submitState : false })
        Toast.fail( res.data.msg);
        return;
      }
    },function(){})
  },
  //去手动核销
  handleDelete(e){
    var that = this;
    var message = "是否要删除该公投？"
    Dialog.confirm({
      title: '删除公投信息',
      message
    }).then(() => {
      that.submitDelOp()
    })
    .catch(() => {
      Dialog.close();
    });
  },
  submitDelOp:function(){
    var that = this;
    fetchApi.httpPost('/survey/delete', {
      survey_id:that.data.survey_id,
    }, function (res) {
      console.log("公投报返回数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        },500)
      }else{
        Toast.fail( res.data.msg);
      }
    },function(){})
  },
  goDelQuestion(e){
    var id = e.currentTarget.dataset.id;
    var that = this;
    var message = "是否要删除该问题选项？"
    Dialog.confirm({
      title: '删除公投选项',
      message
    }).then(() => {
      that.submitDelQuestionOp(id)
    })
    .catch(() => {
      Dialog.close();
    });
  },
  submitDelQuestionOp:function(id){
    var that = this;
    fetchApi.httpPost('/survey/delete/question', {
      survey_id:that.data.survey_id,
      question_id: id,
    }, function (res) {
      console.log("公投报返回数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        that.onHttpInit();
      }else{
        Toast.fail( res.data.msg);
      }
    },function(){})
  },

  submitCheckOp:function(){

    if(this.data.questions.length == 0){
      Toast("未设置公投问题选项，不能提交审核");
      return;
    }

    var that = this;
    fetchApi.httpPost('/survey/regCheck', {
      survey_id:that.data.survey_id,
    }, function (res) {
      console.log("公投报返回数据", res)
      if (res.data.code == 0) {
        Toast.success(res.data.msg);
        setTimeout(function(){
          wx.navigateBack({
            delta: -1,
          })
        },500)
      }else{
        Toast.fail( res.data.msg);
      }
    },function(){})
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作  下拉刷新
   */
  onPullDownRefresh: function () {
    this.onHttpInit()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
})